var app = getApp();
Page({
  data: {
    array: ['微信', '支付宝', '银行卡', '楚阳卡'],
    index: 0,
    allPrice:'',
    orderGoodsList:'',
    orderNmae:'',
    mobile:'',
    address:'',
  },
  onLoad:function(options){
    var that = this;
    const list = JSON.parse(app.globalData.goodsList
)
    console.log(list)
    that.data.orderGoodsList = list
    that.data.allPrice = 0;
    for (const goodslist of list) {
      that.data.allPrice += goodslist.goodsNumber * goodslist.goodsPrice
    }
    
    that.setData({
      goods: list,
      allPrice: that.data.allPrice
    })
    this.Shipping();
  },
  onShow:function(){
    this.Shipping();
  },
  addAdress:function(){
    const userid = app.globalData.userid;
    wx.navigateTo({
      url: '../mine/address/add/add?userid=' + userid,
      success: function (res) {
        wx.setNavigationBarTitle({
          title: "增加收货地址",
        })
      },
      fail: function (res) { },
      complete: function (res) {
      },
    })
  },
  //默认地址展示
  Shipping: function () {
    var that = this;
    var index = this.data.index;
    const userid = app.globalData.userid
    console.log("aa");
    wx.request({
      url: app.globalData.url + 'getMemberAddressByUserId',
      data: {
        userId: userid
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        if (res.data.object[index] == undefined){
          console.log("请添加收货地址")
        }else{
          that.setData({
            userAddress: res.data.object[index],
          });
          that.data.orderName = res.data.object[index].consignee;
          that.data.mobile = res.data.object[index].mobile; 
          that.data.address = res.data.object[index].address
        }
      }
    });
  },
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },
  orderConfirm:function(){
    const orderName = this.data.orderName
    const mobile = this.data.mobile
    const address = this.data.address
    const orderGoodsList = this.data.orderGoodsList
    const userid = app.globalData.userid
    const allPrice = this.data.allPrice
    console.log(orderName + ',' + mobile + ',' + address)
    if (orderName =="" || mobile =='' || address==''){
      wx.showToast({
        title: '请添加收货地址',
        icon: 'none',
        duration: 1000,
        mask: true,
        success: function(res) {},
        fail: function(res) {},
        complete: function(res) {},
      })
    }else{
      wx.request({
        url: app.globalData.url +'submitOrderMultiGoods',
        data: {
          orderType:2,
          userId: userid,
          consignee: orderName,
          mobile: mobile,
          address: address,
          orderGoodsForm: orderGoodsList,
          goodsAmount: allPrice,
          payId:3,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        method: 'GET',
        success: function(res) {

        },
        fail: function(res) {},
        complete: function(res) {},
      })
    }
    wx.navigateTo({
      url: '../orderConfirm/orderConfirm',
      success: function(res) {},
      fail: function (res) {},
      complete: function(res) {},
    })
  },
  /**
   * 修改地址
   */
  changeAddress:function(e){
    console.log(e.currentTarget.dataset)
    if (e.currentTarget.dataset.item == undefined){
      console.log("收货地址为空")
    }else{
      wx.navigateTo({
        url: '../mine/address/address',
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
  }
})